#include "../app_common.h"

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* findIntersectionValues(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize)
{
    int* ret_array = (int*)malloc(sizeof(int) * 2);
    int arrary_val[2] = {0};
    for (int i = 0; i < nums1Size; i++) {
        int nums1_hash_table[101] = {0};
        int nums2_hash_table[101] = {0};

        int val = nums1[i];
        if(nums1_hash_table[val] == 0) {
            nums1_hash_table[val] = 1;
            for(int j = 0; j < nums2Size; j++) {
                if(nums2_hash_table[nums2[j]] == 0) {
                    nums2_hash_table[nums2[j]] = 1;
                    if(val == nums2[j]) {
                        arrary_val[0]++;
                    }
                }
            }

        }
    }

    for (int i = 0; i < nums2Size; i++) {
        int nums1_hash_table[101] = {0};
        int nums2_hash_table[101] = {0};

        int val = nums2[i];
        if(nums2_hash_table[val] == 0) {
            nums2_hash_table[val] = 1;
            for(int j = 0; j < nums1Size; j++) {
                if(nums1_hash_table[nums1[j]] == 0) {
                    nums1_hash_table[nums1[j]] = 1;
                    if(val == nums1[j]) {
                        arrary_val[1]++;
                    }
                }
            }

        }
            
    }

    *returnSize = 2;
    ret_array[0] = arrary_val[0];
    ret_array[1] = arrary_val[1];
    return ret_array;
}

int main(int argc, char* argv[])
{
    // int nums1[]     = {4, 3, 2, 3, 1};
    // int nums2[]     = {2, 2, 5, 2, 3, 6};
    // int nums1[]     = {};
    // int nums2[]     = {2, 4};
    int nums1[]     = {3,4,2,3};
    int nums2[]     = {1,5};
    int return_size = 0;

    int* array = NULL;
    array      = findIntersectionValues(nums1,
                                   sizeof(nums1) / sizeof(nums1[0]),
                                   nums2,
                                   sizeof(nums2) / sizeof(nums2[0]),
                                   &return_size);
    for (int i = 0; i < return_size; i++) {
        printf("%d, ", array[i]);
    }

    printf("\r\n");
    free(array);

    return 0;
}